import 'package:f_enjoy_plus/pages/home/index.dart';
import 'package:f_enjoy_plus/pages/my/index.dart';
import 'package:f_enjoy_plus/core/constants/app_colors.dart';
import 'package:f_enjoy_plus/utils/eventBus.dart';
import 'package:f_enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

class TabBarComp extends StatefulWidget {
  const TabBarComp({super.key});

  @override
  State<TabBarComp> createState() => _TabBarCompState();
}

class _TabBarCompState extends State<TabBarComp> {
  int currentIndex = 0;
  List tabBarList = [
    {
      'title': '首页',
      'defaultIcon': 'assets/tabs/home_default.png',
      'activeIcon': 'assets/tabs/home_active.png',
    },
    {
      'title': '我的',
      'defaultIcon': 'assets/tabs/my_default.png',
      'activeIcon': 'assets/tabs/my_active.png',
    },
  ];

  @override
  void initState() {
    super.initState();
    TokenManager.instance.init();
    eventBus.on<LogoutEvent>().listen((event) {
      Navigator.pushNamed(context, '/login');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: currentIndex,
        children: [HomePage(), MyPage(currentIndex: currentIndex)],
      ),

      bottomNavigationBar: BottomNavigationBar(
        items: barList(),
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        selectedItemColor: AppColors.primary,
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> barList() {
    return tabBarList.map((item) {
      return BottomNavigationBarItem(
        label: item['title'],
        tooltip: item['title'],
        icon: Image.asset(item['defaultIcon'], width: 24, height: 24),
        activeIcon: Image.asset(item['activeIcon'], width: 24, height: 24),
      );
    }).toList();

    // return [
    //   BottomNavigationBarItem(
    //     icon: Icon(Icons.home),
    //     label: '首页',
    //     activeIcon: Icon(Icons.home, color: Colors.blueAccent),
    //   ),
    //   BottomNavigationBarItem(
    //     icon: Icon(Icons.person),
    //     label: '我的',
    //     activeIcon: Icon(Icons.person, color: Colors.blueAccent),
    //   ),
    // ];
  }
}
